// pages/address/addressList/addressList.js
import {
  postRequest,
  getRequest,
  postParamsRequest
} from '../../../api/request';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    themeColor: app.globalData.themeColor,
    pageNo: 1,
    pageSize: 1000,
    loadType: "",
  },

  //点击地址时，跳转编辑
  toAddAddressPage() {
    wx.navigateTo({
      url: '../addAddress/addAddress',
    })
  },
  //编辑地址
  editAddress(e) {
    wx.navigateTo({
      url: '../addAddress/addAddress?id=' + e.currentTarget.dataset.id,
    })
  },

  //获取地址列表
  getMemberAddressList() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    })
    postRequest("/api/vip/member/getMemberAddressList", {
      beid: app.beid,
      memberId: app.myInfo.memberId,
      pageNo: that.data.pageNo,
      pageSize: that.data.pageSize,
    }).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        res.data.data.data.forEach((item, index) => {
          item.isDefault = item.isDefault == 1;
        });
        //判断是否还有数据
        let loadType = "";
        if (res.data.data.totalCount == 0) {
          loadType = "loadNull";
        } else {
          if (Math.ceil(res.data.data.totalCount / that.data.pageNo) <= that.data.pageSize) {
            loadType = "loadOver";
          } else {
            loadType = "loadMore";
          }
        }
        that.setData({
          loadType: loadType,
          dataList: res.data.data.data,
        });
        // 停止下拉刷新效果
        wx.stopPullDownRefresh()
      }
      wx.hideLoading();
    }).catch((err) => {
      // 停止下拉刷新效果
      wx.stopPullDownRefresh()
    });
  },

  //删除地址
  delAddress(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定删除该地址吗？',
      success(res) {
        if (res.confirm) {
          postParamsRequest("/api/vip/member/delMemberAddress", {
            id: e.currentTarget.dataset.id,
            memberId: app.myInfo.memberId,
          }).then((res) => {
            that.getMemberAddressList();
          }).catch((err) => {});
        } else if (res.cancel) {}
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMemberAddressList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getMemberAddressList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})